home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / BSP_WRT.C < prev    next >
C/C++ Source or Header  |  1991-05-18  |  6KB  |  204 lines

  1. /******************************************************************************
  2. * Bsp-Wrt.c - Bspline handling routines - write to file.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #endif /* __MSDOS__ */
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15.  
  16. /******************************************************************************
  17. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  18. * otherwise.                                      *
  19. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  20. ******************************************************************************/
  21. int BspCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  22.                          char *Comment, char **ErrStr)
  23. {
  24.     int i;
  25.     FILE *f;
  26.  
  27.     if ((f = fopen(FileName, "w")) == NULL) {
  28.     *ErrStr = "Fail to open file";
  29.     return FALSE;
  30.     }
  31.     i = BspCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  32.  
  33.     fclose(f);
  34.  
  35.     return i;
  36. }
  37.  
  38. /******************************************************************************
  39. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  40. * otherwise. The file is not closed.                          *
  41. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  42. ******************************************************************************/
  43. int BspCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  44.                                 char **ErrStr)
  45. {
  46.     int i, j, Len, MaxCoord;
  47.  
  48.     if (Comment != NULL) {
  49.     _CagdFprintf(f, Indent, "#\n");
  50.     _CagdFprintf(f, Indent, "# cagd_lib - bspline curve(s) dump.\n");
  51.     _CagdFprintf(f, Indent, "#\n");
  52.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  53.     _CagdFprintf(f, Indent, "#\n");
  54.     }
  55.  
  56.     *ErrStr = NULL;
  57.  
  58.     while (Crvs) {
  59.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  60.  
  61.     if (Crvs -> GType != CAGD_CBSPLINE_TYPE) {
  62.         *ErrStr = "Given curve(s) is (are) not Bspline curve(s)";
  63.         break;
  64.     }
  65.     _CagdFprintf(f, Indent, "[CURVE BSPLINE %d %d %c%c\n",
  66.         Crvs -> Length, Crvs -> Order,
  67.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  68.         MaxCoord + '0');
  69.     Indent += 4;
  70.  
  71.     /* Put out the knot vectors: */
  72.     _CagdFprintf(f, Indent, "[KV");
  73.     Len = Crvs -> Order + Crvs -> Length;
  74.     for (i = 0; i < Len; i++) {
  75.         _CagdFprintf(f, 0, " %s", _CagdReal2Str(Crvs -> KnotVector[i]));
  76.         if (i && i % 8 == 0) {
  77.         _CagdFprintf(f, 0, "\n");
  78.         _CagdFprintf(f, Indent + 3, "");
  79.         }
  80.     }
  81.     _CagdFprintf(f, 0, "]\n");
  82.  
  83.     /* Put out the control polygon. */
  84.     for (i = 0; i < Crvs -> Length; i++) {
  85.         _CagdFprintf(f, Indent, "[");
  86.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  87.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Crvs -> Points[0][i]));
  88.         for (j = 1; j <= MaxCoord; j++) {
  89.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Crvs -> Points[j][i]));
  90.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  91.         }
  92.         _CagdFprintf(f, 0, "]\n");
  93.     }
  94.  
  95.     Indent -= 4;
  96.     _CagdFprintf(f, Indent, "]\n\n");
  97.  
  98.     Crvs = Crvs -> Pnext;
  99.     }
  100.  
  101.     return *ErrStr == NULL;
  102. }
  103.  
  104. /******************************************************************************
  105. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  106. * otherwise.                                      *
  107. ******************************************************************************/
  108. int BspSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  109.                          char *Comment,    char **ErrStr)
  110. {
  111.     int i;
  112.     FILE *f;
  113.  
  114.     if ((f = fopen(FileName, "w")) == NULL) {
  115.     *ErrStr = "Fail to open file";
  116.     return FALSE;
  117.     }
  118.     i = BspSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  119.  
  120.     fclose(f);
  121.  
  122.     return i;
  123. }
  124.  
  125. /******************************************************************************
  126. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  127. * otherwise. The file is not closed.                          *
  128. ******************************************************************************/
  129. int BspSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  130.                                 char **ErrStr)
  131. {
  132.     int i, j, Len, MaxCoord;
  133.     CagdRType *KnotVector;
  134.  
  135.     if (Comment != NULL) {
  136.     _CagdFprintf(f, Indent, "#\n");
  137.     _CagdFprintf(f, Indent, "# cagd_lib - bspline Srf(s) dump.\n");
  138.     _CagdFprintf(f, Indent, "#\n");
  139.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  140.     _CagdFprintf(f, Indent, "#\n");
  141.     }
  142.  
  143.     *ErrStr = NULL;
  144.  
  145.     while (Srfs) {
  146.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  147.  
  148.     if (Srfs -> GType != CAGD_SBSPLINE_TYPE) {
  149.         *ErrStr = "Given surface(s) is (are) not bspline surface(s)";
  150.         break;
  151.     }
  152.     _CagdFprintf(f, Indent, "[SURFACE BSPLINE %d %d %d %d %c%c\n",
  153.         Srfs -> ULength, Srfs -> VLength,
  154.         Srfs -> UOrder, Srfs -> VOrder,
  155.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  156.         MaxCoord + '0');
  157.     Indent += 4;
  158.  
  159.     /* Put out the knot vectors: */
  160.     for (i = 0; i < 2; i++) {
  161.         if (i == 0) {
  162.         KnotVector = Srfs -> UKnotVector;
  163.         Len = Srfs -> ULength + Srfs -> UOrder;
  164.         }
  165.         else {
  166.         KnotVector = Srfs -> VKnotVector;
  167.         Len = Srfs -> VLength + Srfs -> VOrder;
  168.         }
  169.  
  170.         _CagdFprintf(f, Indent, "[KV");
  171.         for (j = 0; j < Len; j++) {
  172.         _CagdFprintf(f, 0, " %s", _CagdReal2Str(KnotVector[j]));
  173.         if (j && j % 8 == 0) {
  174.             _CagdFprintf(f, 0, "\n\t\t");
  175.             _CagdFprintf(f, Indent + 3, "");
  176.         }
  177.         }
  178.         _CagdFprintf(f, 0, "]\n");
  179.     }
  180.  
  181.     /* Put out the control mesh. */
  182.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  183.         if (i && i % Srfs -> ULength == 0)
  184.         _CagdFprintf(f, 0, "\n");    /* Put empty lines between raws. */
  185.  
  186.         _CagdFprintf(f, Indent, "[");
  187.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  188.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Srfs -> Points[0][i]));
  189.         for (j = 1; j <= MaxCoord; j++) {
  190.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Srfs -> Points[j][i]));
  191.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  192.         }
  193.         _CagdFprintf(f, 0, "]\n");
  194.     }
  195.  
  196.     Indent -= 4;
  197.     _CagdFprintf(f, Indent, "]\n\n");
  198.  
  199.     Srfs = Srfs -> Pnext;
  200.     }
  201.  
  202.     return *ErrStr == NULL;
  203. }
  204.